from datetime import timedelta

from odoo import api, fields, tools, models, _
from odoo.exceptions import UserError, ValidationError


class ProductionUov(models.Model):
    _name = 'mrp.production.uom.value'
    _description = 'Production UoM Values'

    production_id = fields.Many2one('mrp.production', "Production", ondelete='cascade')
    uom_id = fields.Many2one('uom.uom', "UoM")
    code = fields.Char('Code', related='uom_id.code')
    uom_value = fields.Float(string='Uom Value', digits=(16, 5))

    @api.onchange('uom_id')
    def _onchange_uom_type(self):
        self.uom_value = 0


class ProductionParamLine(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "mrp.production.param.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id', 'value_ids']
    _description = 'Product Template Param Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    production_id = fields.Many2one('mrp.production', string="Production", ondelete='cascade', required=True, index=True)
    product_id = fields.Many2one(string="Product", comodel_name='product.product', related='production_id.product_id')
    attribute_id = fields.Many2one(
        'product.attribute', string="Attribute", domain="[('limit_type', '=', 'param')]",
        ondelete='restrict', required=True, index=True
    )
    value_id = fields.Many2one(
        'product.attribute.value',
        string="Values", domain="[('attribute_id', '=', attribute_id)]",
    )

    # @api.model_create_multi
    # def create(self, vals_list):
        # self.attribute_id = self.attribute_id.filtered(lambda pav: pav.limit_type == 'param')
        # return super(ProductionParamLine, self).create(vals_list)

    @api.onchange('attribute_id')
    def _onchange_attribute_id(self):
        self.value_id = self.value_id.filtered(lambda pav: pav.attribute_id == self.attribute_id)

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the product %s.") %
    #                 (ptal.attribute_id.display_name, ptal.product_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the product %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.product_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True


class ProductionSpecLine(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "mrp.production.spec.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id', 'value_ids']
    _description = 'Production Specification Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    production_id = fields.Many2one(
        'mrp.production', string="Production", ondelete='cascade', required=True, index=True)
    product_id = fields.Many2one(string="Product", comodel_name='product.product', related='production_id.product_id')
    attribute_id = fields.Many2one(
        'product.attribute', string="Attribute", domain="[('limit_type', '=', 'spec')]",
        ondelete='restrict', required=True, index=True
    )
    value_id = fields.Many2one(
        'product.attribute.value', string="Values", domain="[('attribute_id', '=', attribute_id)]",
    )

    @api.model_create_multi
    def create(self, vals_list):
        self.attribute_id=self.attribute_id.filtered(lambda pav: pav.limit_type == 'spec')
        return super(ProductionSpecLine, self).create(vals_list)

    @api.onchange('attribute_id')
    def _onchange_attribute_id(self):
        self.value_id = self.value_id.filtered(lambda pav: pav.attribute_id == self.attribute_id)

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the product %s.") %
    #                 (ptal.attribute_id.display_name, ptal.product_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the product %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.product_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True


class MrpWorkorderParam(models.Model):
    """Attributes available on product.template with their selected values in a m2m.
    Used as a configuration model to generate the appropriate product.template.attribute.value"""
    _name = "mrp.workorder.param.line"
    _rec_name = 'attribute_id'
    _rec_names_search = ['attribute_id', 'value_id']
    _description = 'Work Order Param Line'
    _order = 'attribute_id, id'

    active = fields.Boolean(default=True)
    workorder_id = fields.Many2one(
        'mrp.workorder', string="Work Order", ondelete='cascade', required=True, index=True)
    attribute_id = fields.Many2one(
        'product.attribute', string="Attribute", domain="[('limit_type', '=', 'route')]",
        ondelete='restrict', required=True, index=True
    )
    # value_ids = fields.Many2many(
    #     'product.attribute.value',
    #     string="Values",
    #     domain="[('attribute_id', '=', attribute_id)]",
    #     relation='product_attribute_value_wordorder_param_line_rel'
    # )

    value_id = fields.Many2one(
        'product.attribute.value', string="Values", domain="[('attribute_id', '=', attribute_id)]"
    )

    thai_name = fields.Char(string="Thai Name", related='attribute_id.thai_name')
    eng_name = fields.Char(string="English Name", related='attribute_id.eng_name')

    z_thai_name = fields.Char(string="Value T Name", related='value_id.thai_name')
    z_eng_name = fields.Char(string="Value E Name", related='value_id.eng_name')

    @api.model_create_multi
    def create(self, vals_list):
        self.attribute_id = self.attribute_id.filtered(lambda pav: pav.limit_type == 'route')
        return super(MrpWorkorderParam, self).create(vals_list)

    @api.onchange('attribute_id')
    def _onchange_attribute_id(self):
        self.value_ids = self.value_ids.filtered(lambda pav: pav.attribute_id == self.attribute_id)

    # @api.constrains('active', 'value_ids', 'attribute_id')
    # def _check_valid_values(self):
    #     for ptal in self:
    #         if ptal.active and not ptal.value_ids:
    #             raise ValidationError(
    #                 _("The attribute %s must have at least one value for the workorder %s.") %
    #                 (ptal.attribute_id.display_name, ptal.workorder_id.display_name)
    #             )
    #         for pav in ptal.value_ids:
    #             if pav.attribute_id != ptal.attribute_id:
    #                 raise ValidationError(
    #                     _("On the workorder %s you cannot associate the value %s with the attribute %s because they do not match.") %
    #                     (ptal.workorder_id.display_name, pav.display_name, ptal.attribute_id.display_name)
    #                 )
    #     return True
#
#
# class MrpWorkorderDrill(models.Model):
#     _name = 's.mrp.workorder.drill'
#     _description = 'Drill Work Center'
#     _rec_name = 'name'
#
#     # 钻带名称
#     name = fields.Char('Drill Name')
#     # 刀号
#     cutter_number = fields.Char('Cutter Number')
#     # 连接层
#     connection_layer = fields.Char('Connection Layer')
#     # 成孔(mm)
#     finished_drill_solt_size = fields.Char('Finished Drill Slot Size')
#     # Slot长(mm)
#     drill_solt_size = fields.Char('Drill Slot Size')
#     # 正公差(mm)
#     drill_tolerance = fields.Char('Drill Tolerance')
#     # 负公差(mm)
#     drill_negative_tolerance = fields.Char('Drill Negative Tolerance')
#     # 钻径(mm)
#     drill_diameter = fields.Char('Drill Diameter')
#     # Slot加工长度(mm)
#     drill_length = fields.Char('Drill Length')
#     # PTH
#     n_pth = fields.Char('Number of PTH')
#     # 加工方式
#     drill_method = fields.Char('Drill Method')
#     # PCS孔数
#     pcs_hole_number = fields.Char('PCS Hole Number')
#     # SET孔数
#     set_hole_number = fields.Char('SET Hole Number')
#     # PNL孔数
#     pnl_hole_number = fields.Char('PNL Hole Number')
#     # 塞孔方式
#     slot_type = fields.Char('Slot Type')
#     # 控深
#     depth_control = fields.Char('Depth Control')
#     # 二钻
#     second_drill = fields.Char('Second Drill')
#     remark = fields.Char('Remark')
#     active = fields.Boolean(default=True)
